package com.project.zhinan.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.project.zhinan.R;
import com.vincent.filepicker.Constant;
import com.vincent.filepicker.activity.ImagePickActivity;
import com.vincent.filepicker.filter.entity.ImageFile;

import java.util.ArrayList;
import java.util.List;

import static com.baidu.mapapi.BMapManager.getContext;
import static com.vincent.filepicker.activity.ImagePickActivity.IS_NEED_CAMERA;

public class RZActivity extends Activity {
    private static final int IMAGECHANGED = 1;
    private static final int MAXIMGNUM = 5;
    private static final int IMAGEUPLOADED = 2;
    private static final int UPLOADSUCCESS = 3;
    private static final int UPLOADFAIL = 4;
    private static final int JSONERROR = 5;
    private static final int NETERROR = 6;
    public ArrayList<String> imgList = new ArrayList<>();
    private EditText et_ad_name;
    CheckBox yue, jifen, daijinquan;
    List<CheckBox> boxList = new ArrayList<>();
    private LinearLayout mImgContainerLinearLayout;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rz);
        et_ad_name = (EditText) findViewById(R.id.et_ad_name);
        yue = (CheckBox) findViewById(R.id.yue);
        jifen = (CheckBox) findViewById(R.id.jifen);
        mImgContainerLinearLayout = (LinearLayout) findViewById(R.id.img_container);
        daijinquan = (CheckBox) findViewById(R.id.daijinquan);
        boxList.add(yue);
        boxList.add(jifen);
        boxList.add(daijinquan);
        Button submit = (Button) findViewById(R.id.bt_fabu);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                et_ad_name.setText("");
                for (CheckBox box : boxList) {
                    box.setChecked(false);
                }
                imgList.clear();
                mImgContainerLinearLayout.removeAllViews();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RZActivity.this, "上传成功！请等待审核！", Toast.LENGTH_SHORT).show();
                    }
                }, 1000);
            }
        });

        ImageButton button = (ImageButton) findViewById(R.id.add_img);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MAXIMGNUM - imgList.size() > 0) {
                    Intent intent1 = new Intent(RZActivity.this, ImagePickActivity.class);
                    intent1.putExtra(IS_NEED_CAMERA, true);
                    intent1.putExtra(Constant.MAX_NUMBER, MAXIMGNUM - imgList.size());
                    startActivityForResult(intent1, Constant.REQUEST_CODE_PICK_IMAGE);
                } else {
                    Toast.makeText(RZActivity.this, "最多上传" + MAXIMGNUM + "张图片", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Constant.REQUEST_CODE_PICK_IMAGE:
                if (resultCode == RESULT_OK) {
                    ArrayList<ImageFile> list = data.getParcelableArrayListExtra(Constant.RESULT_PICK_IMAGE);
                    mImgContainerLinearLayout.removeAllViews();
                    for (ImageFile file : list) {
                        String path = file.getPath();
                        imgList.add(path);
                    }
                    for (String path :
                            imgList) {
                        mImgContainerLinearLayout.addView(addView(path));
                    }
                }
                break;
        }
    }

    private View addView(String path) {
        // TODO 动态添加布局(xml方式)
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LayoutInflater inflater3 = LayoutInflater.from(getContext());
        View view = inflater3.inflate(R.layout.public_img_item, null);
        ImageView imageImageView = (ImageView) view.findViewById(R.id.image);
        Glide.with(getContext()).load(path).into(imageImageView);

        view.setLayoutParams(lp);
        return view;

    }
}
